<template>
  <div>
    <el-button type="primary" style="margin-bottom: 10px" @click="add()"
      >添加</el-button
    >
  </div>
  <el-table
    :data="tableData"
    border
    style="width: 100%"
    :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    row-key="categorySn"
  >
    <el-table-column prop="categoryName" label="类别名称" align="center">
    </el-table-column>
    <el-table-column label="图片" align="center">
      <template #default="scope">
        <el-image
          style="width: 60px; height: 60px"
          :src="scope.row.img"
          :fit="fit"
        ></el-image>
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center" width="260">
      <template #default="scope">
        <el-button @click="update(scope.row)" type="primary">修改</el-button>
        <el-button @click="remove(scope.row)" type="danger">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 添加/修改 -->

  <el-dialog
    title="添加/修改"
    width="500px"
    v-if="addOrUpdateVisible"
    v-model="addOrUpdateVisible"
  >
    <el-form :model="addForm">
      <el-form-item label="父类：" label-width="120px">
        <category-tree
          style="width: 340px"
          :DefaultClear="true"
          :DefaultParentData="parentData"
          :DefaultData="addForm.parentCategorySn"
          @changeData="changeParentCategory"
        ></category-tree>
      </el-form-item>
      <el-form-item label="类别名称：" label-width="120px">
        <el-input
          style="width: 340px"
          v-model="addForm.categoryName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addOrUpdateVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateVisible = false"
          >确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import axios from "axios";
import CategoryTree from "@/components/CategoryTree.vue";
export default {
  components: {
    CategoryTree,
  },
  data() {
    return {
      listQuery: {
        params: "",
      },
      defaultProps: {
        children: "children",
        label: "categoryName",
      },
      addOrUpdateVisible: false,
      addForm: {},
      tableData: [],
      parentData: [],
      value: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      axios
        .post("http://localhost:9999/categories", this.listQuery)
        .then((res) => {
          this.tableData = res.data.data;

          this.tableData = this.$treeData(
            this.tableData,
            "categorySn",
            "parentCategorySn",
            "children"
          );
          this.parentData = this.tableData;
        });
    },
    update(row) {
      (this.title = "修改${row.categoryName}"), (this.addForm = row);
      this.addOrUpdateVisible = true;
    },
    add(){
       this.addOrUpdateVisible = true;
    },
    changeParentCategory(e) {
      console.log(e);
      this.addForm.parentCategorySn = e.categorySn;
    },
    remove(row) {
      this.$confirm(`确认删除${row.categoryName}吗`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$message({
          type: "success",
          message: "删除成功",
        });
      });
    },
  },
};
</script>